#include "mapview.hpp"

MapView::MapView(QWidget *parent, QStatusBar* statusBar) : QGraphicsView(parent), _statusBar(statusBar) {
	_scene = new QGraphicsScene;
	setScene(_scene);

	_back = new QGraphicsPixmapItem;
	_back->setPixmap(_back_pixmap);
	_back->setPos(0,0);
	_back->show();
	_scene->addItem(_back);

	// Shape
	setFrameShape(QFrame::Box);

	// On accepte les drops
	setAcceptDrops(true);

	// On Track la souris
	setMouseTracking(true);
}


void MapView::changeBackground(QString &path_background) {
	_back_pixmap.load(path_background);
	_scene->setSceneRect(0, 0, _back_pixmap.width(), _back_pixmap.height());
	setMaximumSize(_back_pixmap.width(), _back_pixmap.height());
	_back->setPixmap(_back_pixmap);
}

void MapView::clear() {
	_scene->clear();
	_back = new QGraphicsPixmapItem;
	_back->setPos(0, 0);
	_back->show();
	_scene->addItem(_back);
}

// Methode Get
QGraphicsScene* MapView::getScene() { return _scene; }
QGraphicsPixmapItem* MapView::getFond() { return _back; }

void MapView::canSave() {
	emit changed();
}

// Event pour le Drag and Drop
void MapView::mousePressEvent(QMouseEvent *event) {
	if(event->button() == Qt::RightButton) return;

	MapItem *child = static_cast<MapItem*>(_scene->itemAt(mapToScene(event->pos())));
	if(!child || child == _back)
		return;

	QPixmap pixmap = child->pixmap();
	QByteArray itemData;
	QDataStream dataStream(&itemData, QIODevice::WriteOnly);
	QPointF offset = mapToScene(event->pos() - child->pos().toPoint());

	dataStream << pixmap << offset.toPoint();

	QMimeData *mimeData = new QMimeData;
	mimeData->setData("application/sprite", itemData);

	QDrag *drag = new QDrag(this);
	drag->setMimeData(mimeData);
	drag->setPixmap(pixmap);
	drag->setHotSpot(offset.toPoint());
	_scene->removeItem(child);


	if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
		child->hide();
	else {
		child->show();
		child->setPixmap(pixmap);
	}
}

void MapView::dropEvent(QDropEvent *event) {
	if(event->mimeData()->hasFormat("application/sprite")) {
		QByteArray itemData = event->mimeData()->data("application/sprite");
		QDataStream dataStream(&itemData, QIODevice::ReadOnly);

		QPixmap pixmap;
		QPoint offset;
		dataStream >> pixmap >> offset;
		QPointF point = mapToScene(event->pos());

		MapItem *item = new MapItem;
		item->setPixmap(pixmap);
		_scene->addItem(item);
		emit changed();
		item->setPos(point - offset);

		if(event->source() == this) {
			event->setDropAction(Qt::MoveAction);
			event->accept();
		}
		else event->acceptProposedAction();
	}
	else event->ignore();
}

void MapView::dragEnterEvent(QDragEnterEvent *event) {
	if(event->mimeData()->hasFormat("application/sprite")) {
		if(event->source() == this) {
			event->setDropAction(Qt::MoveAction);
			event->accept();
		}
		else event->acceptProposedAction();

	}
	else event->ignore();
}

void MapView::dragMoveEvent(QDragMoveEvent *event) {
	if(event->mimeData()->hasFormat("application/sprite")) {
		if(event->source() == this) {
			event->setDropAction(Qt::MoveAction);
			event->accept();
		}
		else event->acceptProposedAction();

	}
	else event->ignore();
}

void MapView::mouseMoveEvent(QMouseEvent *event) {
	QPointF coordonnes = mapToScene(event->pos());
	_statusBar->showMessage("X: " + QString::number(coordonnes.x()) + " / Y: " + QString::number(coordonnes.y()));
	QGraphicsView::mouseMoveEvent(event);
}
